package com.zlm.linked;

import java.util.Objects;

/**
 * @author zlm
 * @date 2025/4/29
 */
public class GroupLinked {

    public static void main(String[] args) {
        Node<Integer> head = new Node<>(6,null);
        head.next = new Node<>(2, null);
        head.next.next = new Node<>(5,null);
        head.next.next.next = new Node<>(7,null);
        head.next.next.next.next = new Node<>(5,null);
        head.next.next.next.next.next = new Node<>(1,null);
        head.next.next.next.next.next.next = new Node<>(1,null);

        head = groupLinked(head, 5);
        head.printNode();
    }


    public static Node<Integer> groupLinked(Node<Integer> head, int x) {
        if (head == null) {
            return null;
        }

        Node<Integer> smallHead = new Node<>(null, null);
        Node<Integer> smallCur = smallHead;
        Node<Integer> bigHead = new Node<>(null, null);
        Node<Integer> bigCur = bigHead;
        Node<Integer> cur = head;
        while (cur != null){
            if(cur.value < x){
                smallCur.next = cur;
                smallCur = cur;
            }else{
                bigCur.next = cur;
                bigCur = cur;
            }
            cur = cur.next;
        }
        bigCur.next = null;
        smallCur.next = bigHead.next;
        return smallHead.next;
    }
}
